﻿using Android.Content;
using DataProcess.Authable;
using System;

namespace BlackBoxForU.Feature
{
    class Limited
    {
        public static string Key
        {
            get => "TbsAndRyq";
        }
        public static string Setting
        {
            get => "Limited";
        }
        public static string InitCode
        {
            get;
            private set;
        }
        static bool HasInit(Context context)
        {
            using (ISharedPreferences i = context.GetSharedPreferences(Setting, FileCreationMode.Private))
            {
                if (i != null)
                {
                    return i.GetBoolean("HasInit", false);
                }
                else
                    return false;
            }
        }
        public static DateTime InitDate
        {
            get;
            private set;
        }
        public static string genInitCode(long tick)
        {
            DataProcess.Authable.HMACbasedOneTimeAuthenticator authenticator = new HMACbasedOneTimeAuthenticator() { Digit = 8 };
            authenticator.OnResult += Authenticator_OnResult;
            authenticator.Key = Key;
            authenticator.CodingLevel = DataProcess.Encryption.HMACMaker.Level.SHA256;
            InitDate = new DateTime(tick, DateTimeKind.Local);
            string code = authenticator.Generate(InitDate.Ticks);
            return code;
        }
        public static void ReSetInitCode(Context context, long tick)
        {
            ISharedPreferences i = context.GetSharedPreferences(Setting, FileCreationMode.Private);
            var e = i.Edit();
            InitCode = genInitCode(tick);
            e.PutString("InitCode", InitCode);
            e.PutLong("InitDate", tick);
            e.Commit();
        }
        static string Init(Context context)
        {
            ISharedPreferences i = context.GetSharedPreferences(Setting, FileCreationMode.Private);
            var e = i.Edit();
            DataProcess.Authable.HMACbasedOneTimeAuthenticator authenticator = new HMACbasedOneTimeAuthenticator() { Digit = 8 };
            authenticator.OnResult += Authenticator_OnResult;
            authenticator.Key = Key;
            authenticator.CodingLevel = DataProcess.Encryption.HMACMaker.Level.SHA256;
            InitDate = DateTime.Now;
            string code = authenticator.Generate(InitDate.Ticks);
            e.PutString("InitCode", code);
            e.PutBoolean("HasInit", true);
            e.PutLong("InitDate", InitDate.Ticks);
            e.PutInt("More", 0);
            e.PutLong("LastRightUse", -1);
            e.Commit();
            return code;
        }

        private static string Authenticator_OnResult(byte[] rawdata, string nowcode)
        {
            string result = "";
            for (int i = 17; i < 21; i++)
            {
                result += DataProcess.Mathematics.NumberCase.ExchangeFromTen(rawdata[i], 16, 2);
            }
            return result;
        }
        public static int getMoreTime(Context context)
        {
            ISharedPreferences i = context.GetSharedPreferences(Setting, FileCreationMode.Private);
            return i.GetInt("More", 0);
        }
        public static void MoreTimeChange(Context context, int change)
        {
            int r = getMoreTime(context) + change;
            ISharedPreferences i = context.GetSharedPreferences(Setting, FileCreationMode.Private);
            i.Edit().PutInt("More", r).Commit();
        }
        public static void CheckForLimit(Context context)
        {
            if (HasInit(context))
            {
                InitCode = context.GetSharedPreferences(Setting, FileCreationMode.Private).GetString("InitCode", "无效身份码");
                long d = context.GetSharedPreferences(Setting, FileCreationMode.Private).GetLong("InitDate", 0);
                InitDate = new DateTime(d, DateTimeKind.Local);
            }
            else
                InitCode = Init(context);
        }
        public static void UseForRight(Context context)
        {
            ISharedPreferences i = context.GetSharedPreferences(Setting, FileCreationMode.Private);
            i.Edit().PutLong("LastRightUse", DateTime.Now.Ticks).Commit();
        }
        public static bool CheckForRights(Context context)
        {
            ISharedPreferences i = context.GetSharedPreferences(Setting, FileCreationMode.Private);
            long d = i.GetLong("LastRightUse", -1);
            if (d > 0)
            {
                DateTime last = new DateTime(d, DateTimeKind.Local).Date;
                DateTime n = DateTime.Now.Date;
                return n > last;
            }
            return true;
        }
    }
}